﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Yunchang.ILUI
{
    public abstract class UIItemExtendRelateView<T> : UIItemRelateView<T>, UIExtendPart where T : UIViewBase
    {
        private UIExtendCtrl extendCtrl;

        //当前部件部件
        public abstract UIPartLayout layout { get; }

        //当前部件索引
        public abstract int partIndex { get; }

        //子部件数量
        public abstract int childPartCount { get; }

        public override void Awake()
        {
            if (childPartCount > 0)
                extendCtrl = UIExtendCtrl.New(childPartCount, (int)UIPartLayout.SIZE);
            base.Awake();
        }

        /// <summary>
        /// 初始化部件
        /// </summary>
        /// <param name="args"></param>
        public virtual void InitPart(object[] args)
        {
            if (extendCtrl == null) return;
            extendCtrl.ResetDefaultArgs(args);
            extendCtrl.InitCurExtendParts();
        }

        /// <summary>
        /// 重置部件
        /// </summary>
        public virtual void ResetPart()
        {
            if (extendCtrl == null) return;
            extendCtrl.ResetCurExtendParts();
        }

        protected override void AddItemComplete(UIItem item)
        {
            base.AddItemComplete(item);

            if (extendCtrl == null) return;
            UIExtendPart part = item as UIExtendPart;
            if (part == null)
            {
                GLog.Error(" {0} add extend error. item is null or is not UIExtendPart.", GetType().Name);
                return;
            }

            extendCtrl.SetExtendPart(part);
            extendCtrl.ShowExtendPart(part);
        }

        /// <summary>
        /// 显示附加子部件
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="partIndex"></param>
        /// <param name="parent"></param>
        /// <param name="resourceID"></param>
        public void ShowExtendPart<TK>(int partIndex, RectTransform parent, int resourceID) where TK : UIItem, new()
        {
            if (extendCtrl == null) return;
            UIExtendPart part = extendCtrl.GetExtendPart(partIndex);
            if (part == null)
                AddItem<TK>(parent, resourceID);
            else
                extendCtrl.ShowExtendPart(part);
        }

        /// <summary>
        /// 显示附加子部件
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="partIndex"></param>
        /// <param name="parent"></param>
        /// <param name="resourceID"></param>
        public void ShowExtendPartWithArgs<TK>(int partIndex, RectTransform parent, int resourceID, params object[] args) where TK : UIItem, new()
        {
            if (extendCtrl == null) return;
            extendCtrl.ResetArgs(partIndex, args);
            ShowExtendPart<TK>(partIndex, parent, resourceID);
        }

        /// <summary>
        /// 隐藏附加子部件
        /// </summary>
        /// <param name="layout"></param>
        public void HideExtendPart(UIPartLayout layout)
        {
            if (extendCtrl == null) return;
            extendCtrl.HideExtendPart(layout);
        }

        public override void OnDestroy()
        {
            if (extendCtrl != null)
            {
                extendCtrl.Clear();
                extendCtrl = null;
            }

            base.OnDestroy();
        }
    }
}